﻿//-----------------------------------------------------------------------
// <copyright file="SearchViewModel.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.Threading;
using ConsoleHoster.Common.ViewModel;
using System;
using System.Windows.Input;

namespace ConsoleHoster.ViewModel
{
	public class SearchViewModel : ViewModelBase
	{
		public event Action<string> FindNext;

		private bool searchEnabled = false;
		private string textToFind = null;
		private ICommand closeSearchCommand = null;
		private ICommand findNextCommand = null;

		public SearchViewModel(IDispatcher argDispatcher, ILogger argLogger)
			: base(argDispatcher, new ComponentLogger("SearchViewModel", argLogger))
		{
		}

		public void CloseSearch()
		{
			this.SearchEnabled = false;
			this.TextToFind = String.Empty;
		}

		private void OnFind()
		{
			Action<string> temp = this.FindNext;
			if (temp != null)
			{
				temp(this.TextToFind);
			}
		}

		public bool SearchEnabled
		{
			get
			{
				return this.searchEnabled;
			}
			set
			{
				if (value != this.searchEnabled)
				{
					this.searchEnabled = value;
					this.NotifyPropertyChange("SearchEnabled");
				}
			}
		}

		public string TextToFind
		{
			get
			{
				return this.textToFind;
			}
			set
			{
				if (value != this.textToFind)
				{
					this.textToFind = value;
					this.NotifyPropertyChange("TextToFind");
				}
			}
		}

		public ICommand CloseSearchCommand
		{
			get
			{
				if (this.closeSearchCommand == null)
				{
					this.closeSearchCommand = new RelayCommand(o => this.CloseSearch());
				}
				return this.closeSearchCommand;
			}
		}

		public ICommand FindNextCommand
		{
			get
			{
				if (this.findNextCommand == null)
				{
					this.findNextCommand = new RelayCommand(o => this.OnFind());
				}
				return this.findNextCommand;
			}
		}
	}
}